package cn.zmh.framework.util;

import java.util.HashMap;
import java.util.Map;
import java.lang.reflect.Method;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/***
 * 根据POJO对象生成mybatis调用存储过程的map
 * 
 * @param Object
 *            pojo
 * @return Map pojoMap
 */
public class Pojo2Map {
	private static Logger logger = LoggerFactory.getLogger(Pojo2Map.class);

	public static Map<String, Object> getMap(Object pojo) {
		Map<String, Object> pojoMap = new HashMap<String, Object>();
		String key;
		int index;
		try {
			Class<? extends Object> c = pojo.getClass();
			Method m[] = c.getDeclaredMethods();
			for (int i = 0; i < m.length; i++) {
				if (m[i].getName().indexOf("get") == 0) {
					index = m[i].getName().indexOf("get");
					key = "i" + m[i].getName().substring(index + 3);
					pojoMap.put(key, m[i].invoke(pojo, new Object[0]));
				}
			}
		} catch (Throwable e) {
			logger.error(StackTraceUtil.getStackTrace(e));

		}
		pojoMap.put("out_flag", "");
		pojoMap.put("out_msg", "");
		return pojoMap;
	}
	
	
	public static Map<String, String> getMapString(Object pojo) {
		Map<String, String> pojoMap = new HashMap<String, String>();
		String key;
		int index;
		try {
			Class<? extends Object> c = pojo.getClass();
			Method m[] = c.getDeclaredMethods();
			for (int i = 0; i < m.length; i++) {
				if (m[i].getName().indexOf("get") == 0) {
					index = m[i].getName().indexOf("get");
					key = "i" + m[i].getName().substring(index + 3);
					pojoMap.put(key, (String) m[i].invoke(pojo, new Object[0]));
				}
			}
		} catch (Throwable e) {
			logger.error(StackTraceUtil.getStackTrace(e));

		}
		pojoMap.put("out_flag", "");
		pojoMap.put("out_msg", "");
		return pojoMap;
	}

	
	public static Map<String, String> getMapString2(Object pojo) {
		Map<String, String> pojoMap = new HashMap<String, String>();
		String key;
		String val;
		int index;
		try {
			Class<? extends Object> c = pojo.getClass();
			Method m[] = c.getDeclaredMethods();
			for (int i = 0; i < m.length; i++) {
				if (m[i].getName().indexOf("get") == 0) {
					index = m[i].getName().indexOf("get");
					key = m[i].getName().substring(index + 3);
					
					key=key.substring(0,1).toLowerCase()+key.substring(1,key.length());		
					 Object obj= m[i].invoke(pojo, new Object[0]);
					 
					if( StringUtil.isObjNotNull(obj)){
						if( obj.getClass().isArray()){
							 obj=null;
						 }
					 }
					val=String.valueOf(obj);
					if(StringUtil.isEqualObj("null", val)||StringUtil.isEqualObj("NULL", val)){
						val="";
					}
					pojoMap.put(key, val);
				}
			}
		} catch (Throwable e) {
			logger.error(StackTraceUtil.getStackTrace(e));

		}
		pojoMap.put("out_flag", "");
		pojoMap.put("out_msg", "");
		return pojoMap;
	}
}
